const MarkdownIt = require('markdown-it')
const MarkdownContainer = require('markdown-it-container')
const md = new MarkdownIt({ html: true, linkify: true, typographer: true })

const use = []
use.push([
  require('markdown-it-anchor'), {
    level: 2, // 添加超链接锚点的最小标题级别, 如: #标题 不会添加锚点
    permalink: true, // 开启标题锚点功能
    permalinkBefore: true // 在标题前创建锚点
  }
])

use.push([ require('markdown-it-sup') ])
use.push([ require('markdown-it-sub') ])
use.push([
  require('markdown-it-include'),
  { root: 'packages/', includeRe: /#include(.+)/, bracesAreOptional: true }
])

use.push([ MarkdownContainer, 'tip', {
  validate: value => value.trim() === 'tip',
  render (tokens, idx) {
    if (tokens[idx].nesting === 1) {
      return '<div class="md-it-alter md-it-alter-tip">tip：'
    }
    return '</div>'
  }
} ])
use.push([ MarkdownContainer, 'info', {
  validate: value => value.trim() === 'info',
  render (tokens, idx) {
    if (tokens[idx].nesting === 1) {
      return '<div class="md-it-alter md-it-alter-info">info'
    }
    return '</div>'
  }
} ])
use.push([ MarkdownContainer, 'warning', {
  validate: value => value.trim() === 'warning',
  render (tokens, idx) {
    if (tokens[idx].nesting === 1) {
      return '<div class="md-it-alter md-it-alter-warning">warning：'
    }
    return '</div>'
  }
} ])

use.push([ MarkdownContainer, 'demo', {
  validate: value => value.trim().match(/^demo\s*(.*)$/),
  render (tokens, idx) {
    const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/)
    if (tokens[idx].nesting === 1) {
      const description = m && m.length > 1 ? m[1] : ''
      return `<me-demo-block><template #description>${md.render(description)}</template>`
    }
    return '</me-demo-block>'
  }
} ])
module.exports = {
  raw: true,
  preventExtract: true,
  use
}
